package 剑指offer;

/**
 *
 *
 * 题目：定义一个函数，输入一个链表的头结点，反转该链表并输出反转后链表的头结点
 *
 * Created by sult on 2018/9/28.
 */
public class 翻转链表 {

    public static class Node{
        private String value;
        private Node nextNode;
    }


    public static Node reversalNode(Node head){
        Node root = new Node();
        root.nextNode=null;

        // 存放要输处理的下一个节点
        Node nextNode;

        while(head!=null){
            // 记录要处理的下一个节点
            nextNode=head.nextNode;
            //当前节点的下一个结点指向逻辑头节点的下一个结点
            head.nextNode=root.nextNode;

            root.nextNode=head;

            head=nextNode;
        }

        return root.nextNode;
    }



    public static void printList(Node node){
        while (node != null) {
            System.out.print(node.value + "->");
            node = node.nextNode;
        }
        System.out.println("null");

    }


    public static void main(String[] args) {
        Node head = new Node();
        head.value = "A";

        head.nextNode = new Node();
        head.nextNode.value = "B";

        head.nextNode.nextNode = new Node();
        head.nextNode.nextNode.value = "C";




        printList(head);
        head = reversalNode(head);
        printList(head);
//        head = reversalNode(head);
//        printList(head);

    }


}
